#include <iostream>
#include <memory>
#include <string>
using std::cout;
using std::endl;
using std::unique_ptr;
using std::shared_ptr;
using std::string;

void test0(){
    string msg("hello,world\n");
    FILE * fp = fopen("wd.txt","a+");
    fwrite(msg.c_str(),1,msg.size(),fp);
    fclose(fp);
}

struct FILECloser
{
    void operator()(FILE * fp){
        if(fp){
            fclose(fp);
            cout << "fclose(fp)" << endl;
        }
    }
};

void test1(){
    string msg("hello,world\n");
    unique_ptr<FILE,FILECloser> up(fopen("wd.txt","a+"));
    //get函数可以从智能指针中获取到裸指针
    fwrite(msg.c_str(),1,msg.size(),up.get());
    /* fclose(up.get()); */
}

void test2(){
    string msg("hello,world\n");
    FILECloser fc;
    shared_ptr<FILE> sp(fopen("wd.txt","a+"),fc);
    fwrite(msg.c_str(),1,msg.size(),sp.get());
    /* fclose(sp.get()); */
}

int main(void){
    test2();
    return 0;
}
